import router from '@/router'
import nprogress from 'nprogress'
import store from '@/store'
import 'nprogress/nprogress.css'

const whiteList = ['LoginPage', '404Page']

// 前置路由守卫
router.beforeEach((to, from, next) => {
  // 显示进度条
  nprogress.start()
  const token = store.getters.token
  if (!token && !whiteList.includes(to.name)) {
    // 重定向到登录页
    next({ name: 'LoginPage' })
    nprogress.done()
  } else {
    next()
  }
})

// 后置路由守卫
router.afterEach(() => {
  // 关闭进度条
  nprogress.done()
})
